﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twi.COMMON.Core;
using Twi.DataAccess.OutInterface;
using System.Data;
using System.Xml.Linq;
using System.Web;
using System.Collections.Specialized;

namespace Twi.COMMON.Page
{
    public abstract class TwiPageHelper
    {
        public abstract IPageCommon pageCommon
        {
            get;
        }

        public TwiReturn ExistField(string tableName, string fieldName, string fieldValue)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                twi.data = pageCommon.ExistField(tableName, fieldName, fieldValue);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn ExistField(string tableName, string fieldName, string fieldValue, string id, string idField = "FID")
        {
            idField = string.IsNullOrEmpty(idField) ? "FID" : idField;
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                twi.data = pageCommon.ExistField(tableName, fieldName, fieldValue, id, idField);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }


        public TwiReturn ExistEditSort(string tableName, int? sort, string id, string filterField, string filterFieldValue, string sortField = "FSort", string idField = "FID")
        {
            idField = string.IsNullOrEmpty(idField) ? "FID" : idField;
            sortField = string.IsNullOrEmpty(sortField) ? "FSort" : sortField;
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                twi.data = pageCommon.ExistEditSort(tableName, sort, id, filterField, filterFieldValue, sortField, idField);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn ExistEditSort(string tableName, int? sort, string id, string sortField = "FSort", string idField = "FID")
        {
            idField = string.IsNullOrEmpty(idField) ? "FID" : idField;
            sortField = string.IsNullOrEmpty(sortField) ? "FSort" : sortField;
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                twi.data = pageCommon.ExistEditSort(tableName, sort, id, sortField, idField);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn ExistSort(string tableName, int? sort, string filterField, string filterFieldValue, string sortField = "FSort")
        {
            sortField = string.IsNullOrEmpty(sortField) ? "FSort" : sortField;
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                twi.data = pageCommon.ExistSort(tableName, sort, filterField, filterFieldValue, sortField);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn ExistSort(string tableName, int? sort, string sortField = "FSort")
        {
            sortField = string.IsNullOrEmpty(sortField) ? "FSort" : sortField;
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                twi.data = pageCommon.ExistSort(tableName, sort, sortField);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn GetMaxSort(string tableName, string filterField, string filterFieldValue, string sortField = "FSort")
        {
            sortField = string.IsNullOrEmpty(sortField) ? "FSort" : sortField;
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                twi.data = pageCommon.GetMaxSort(tableName, filterField, filterFieldValue, sortField);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn GetMaxSort(string tableName, string sortField = "FSort")
        {
            sortField = string.IsNullOrEmpty(sortField) ? "FSort" : sortField;
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                twi.data = pageCommon.GetMaxSort(tableName, sortField);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn GetCountByCondition(string data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                twi.totalCount = pageCommon.GetCountByCondition(data);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }


        public TwiReturn Delete(string tableName, string id, string idField = "FID")
        {
            idField = string.IsNullOrEmpty(idField) ? "FID" : idField;
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);                
                idField = string.IsNullOrEmpty(idField) ? "FID" : idField;
                pageCommon.Delete(tableName, id, idField);

                //写日志
                WriteSimpleLog();

                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn DeleteBySetFlag(string data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                pageCommon.DeleteBySetFlag(data);

                //写日志
                WriteSimpleLog();

                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public DataTable GetAll(string tableName, string selectFields = "*")
        {
            selectFields = string.IsNullOrEmpty(selectFields) ? "*" : selectFields;
            return pageCommon.GetAll(tableName, selectFields);
        }

        public DataTable GetByID(string tableName, string id, string idField = "FID", string selectFields = "*")
        {
            idField = string.IsNullOrEmpty(idField) ? "FID" : idField;
            selectFields = string.IsNullOrEmpty(selectFields) ? "*" : selectFields;
            return pageCommon.GetByID(tableName, id, idField, selectFields);
        }

        public DataTable GetBySimpleCondition(string data)
        {
            return pageCommon.GetBySimpleCondition(data);
        }


        public DataTable GetPagingDataByCondition(string data, PagingCondition paging = null)
        {
            return pageCommon.GetPagingDataByCondition(data, paging);
        }

        public DataTable GetByComplexCondition(string data, PagingCondition paging = null)
        {
            return pageCommon.GetByComplexCondition(data, paging);
        }

        public TwiReturn NewOrEdit(string data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                twi.id = pageCommon.NewOrEdit(data);

                //写日志
                WriteSimpleLog();

                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn SimpleNew(string data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                twi.id = pageCommon.SimpleNew(data);

                //写日志
                WriteSimpleLog();

                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }


        public TwiReturn SimpleEdit(string data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                pageCommon.SimpleEdit(data);

                //写日志
                WriteSimpleLog();

                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public DataTable GetBySQLMap(string data)
        {
            return pageCommon.GetBySQLMap(data);
        }

        public DataTable GetBySQLMap2(SQLMapModel model)
        {
            return pageCommon.GetBySQLMap(model);
        }
        /// <summary>
        /// 根据SQL-MAP查询分页数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetPagingDataBySQLMap(string data, PagingCondition paging = null)
        {
            return pageCommon.GetPagingDataBySQLMap(data, paging);
        }

        public TwiReturn GetXmlBySQLMap(string data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                twi.data = pageCommon.GetXmlBySQLMap(data);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn SimpleInsertXml(XElement data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                twi.data = pageCommon.SimpleInsertXml(data);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn SimpleUpdateXml(XElement data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                twi.data = pageCommon.SimpleUpdateXml(data);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;

        }

        public TwiReturn DeleteXml(XElement data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                twi.data = pageCommon.DeleteXml(data);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }
        public TwiReturn GetSingleXml(XElement data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                twi.data = pageCommon.GetSingleXml(data);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }
        public TwiReturn GetXmlBySQLMap2(string model)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                twi.data = pageCommon.GetXmlBySQLMap(model);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn ExecuteBySQLMap(string data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                pageCommon.ExecuteBySQLMap(data);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        public TwiReturn GetXElmentBySimpleCondition(string data)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                DataTable dt = pageCommon.GetBySimpleCondition(data);
                twi.data = TwiDataTableHelper.DataTableToXElement(dt);
                twi.success = true;
            }
            catch (Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        /// <summary>
        /// 是否不需要写日志，等于1时不往数据库中写简单日志（BIM_SIMPLELOG）
        /// </summary>
        private string TwiIgnoreSimpleLog = TwiConfigHelper.GetAppSettingValue("TwiIgnoreSimpleLog");
        /// <summary>
        /// simpleLog:
        /// {
        ///     FREMARK:'',
        ///     FMN:'',
        ///     FFUNCTIONNAME:''
        /// }
        /// </summary>
        public void WriteSimpleLog() {
            if (TwiIgnoreSimpleLog != "1" && UserContext.Request !=null) //写日志
            {
                string simpleLog = UserContext.Request.Form["simpleLog"];
                if (simpleLog == null) {
                    return;
                }
                Dictionary<string, object> dict = new Dictionary<string, object>();
                dict.Add("FOPTORCODE", UserContext.CurrentUser.FNAME);
                dict.Add("FOPTORNAME", UserContext.CurrentUser.FREALNAME);

                NameValueCollection nvc = UserContext.Request.Form;
                if (nvc != null && nvc.Count > 0)
                {
                    List<Dictionary<string, string>> listDict = new List<Dictionary<string, string>>();
                    for (int i = 0; i < nvc.Keys.Count; i++)
                    {
                        Dictionary<string, string> dictKeyValue = new Dictionary<string, string>();
                        dictKeyValue.Add(nvc.Keys[i], nvc[nvc.Keys[i]]);
                        listDict.Add(dictKeyValue);
                    }
                    string requestData = JSSerialize.Serialize(listDict);
                    dict.Add("FREQUESTDATA", requestData);
                }
                
                if (simpleLog != null)
                {
                    var dictSimpleLog = JSSerialize.Deserialize<Dictionary<string, object>>(simpleLog);
                    dict = TwiDictionaryHelper<string>.MergeTwoDicts(dict, dictSimpleLog);
                }
                pageCommon.WriteSimpleLog(dict);
            }
        }

        /// <summary>
        /// 获取数据库时间
        /// </summary>
        /// <returns></returns>
        public TwiReturn GetDBNowDateTime() 
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                twi.data = pageCommon.GetDBNowDateTime();
                twi.success = true;
            }
            catch (Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

    }
}
